/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.nokia.maps.map.MapCanvas;
import com.nokia.maps.common.*;
import com.nokia.maps.map.MapCircle;
import com.nokia.maps.map.MapRectangle;
import com.nokia.maps.common.GeoBoundingBox;

/**
 * @author juksilve
 */
public class MoreMapContent extends MIDlet 
implements CommandListener
{

    private final Command menuCommand;
    private final Command exitCommand;
    private final Command backCommand;
    private final Command selectCommand;
    private final Command selListCommand;
    
    int cuntter = 0;
    int selllected = 0;
    Display display = null;
    List menu = null; // main menu
    List selList = null; // selection menu
    MapCanvas mapCanvas = null; 
    
    MapCircle myCircle = null;    
    MapRectangle rectnagle = null;
    boolean chnagedPos = false;
             
    public MoreMapContent()
    {
        menuCommand = new Command("Options", Command.ITEM, 1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        backCommand = new Command("Back", Command.BACK, 1);
        selectCommand = new Command("Open", Command.ITEM, 1); 
        selListCommand = new Command("Select", Command.ITEM, 1); 
     
    }
    
    public void startApp() {
        // Get your own app_id and token by registering at
	// https://api.developer.nokia.com/ovi-api/ui/registration
//	ApplicationContext.getInstance().setAppID("My-Id");
//	ApplicationContext.getInstance().setToken("My-Tokern");

        menu = new List("Menu Items", Choice.IMPLICIT);
        menu.append("Circle", null);
        menu.append("Rectangle", null);
  
        
        menu.setSelectCommand(selectCommand);
        menu.addCommand(backCommand);
        menu.setCommandListener(this);
        
	display = Display.getDisplay(this);
        mapCanvas = new MapCanvas(display) {
            public void onMapUpdateError(String string, Throwable thrwbl, boolean bln) {}
            public void onMapContentComplete() {}
        };
        display.setCurrent(mapCanvas);

        mapCanvas.getMapDisplay().setZoomLevel(11, 0, 0);    
        mapCanvas.getMapDisplay().setCenter(new GeoCoordinate(60.30, 24.70, 0));
        
        mapCanvas.setTitle("More Map content");
        mapCanvas.addCommand(exitCommand);
        mapCanvas.addCommand(menuCommand);
        mapCanvas.setCommandListener(this);
           
        // add own component to get key & touch events
        mapCanvas.getMapDisplay().addMapComponent(new CustomMapComponent(mapCanvas.getMapFactory()));
        
        System.out.println("We are started");          
    }
    
    
    public void commandAction(Command command, Displayable displayable)
    {
        System.out.println("command action: " + command.getLabel());

        if (command == exitCommand){
            System.out.println("Exit");
            notifyDestroyed();
        }else if(command == backCommand){
            System.out.println("We go back to canvas");
            display.setCurrent(mapCanvas);
        }else if(command == menuCommand){
            System.out.println("We show menu");
            display.setCurrent(menu);
        }else if(command == selectCommand){

            System.out.println("We see what is selected with menu");
            
            selllected = menu.getSelectedIndex();

            selList = new List("Edit", Choice.IMPLICIT);
            selList.setSelectCommand(selListCommand);
            selList.addCommand(backCommand);
            selList.setCommandListener(this);
            
            selList.append("Add",null);
            selList.append("Z-Top",null);
            selList.append("Z-Bottom",null);
            selList.append("Toggle visibility",null);
            selList.append("Zoom to",null);
            
            if(selllected == 0){
               System.out.println("Circle");
               selList.setTicker(new Ticker("Circle"));
               
               selList.append("Remove",null);
               selList.append("Toggle position",null);
               selList.append("Toggle size",null);
               
               display.setCurrent(selList);
            }else if(selllected == 1){
                System.out.println("Rectangle");
                selList.setTicker(new Ticker("Rectangle"));
  
                selList.append("Remove",null);
                
                display.setCurrent(selList);
            }else {
                Alert alert = new Alert("", "Uknown Selection", null, AlertType.INFO);    
                display.setCurrent(alert);
            }
                    
        }else if(command == selListCommand){
            
            System.out.println("Open command selection");
            
            int sellected = selList.getSelectedIndex();
            
            if(selllected == 0){ //Markers
                
                if(sellected == 0){ //Add

                    if(myCircle == null){
                                                    
                        myCircle = mapCanvas.getMapFactory().createMapCircle(5000.0,new GeoCoordinate(60.30, 24.70,0));
                        myCircle.setColor(0xAAFF0506);
                            
                        mapCanvas.getMapDisplay().addMapObject( myCircle );
                        System.out.println("myCircle added");
                    }else{//already added
                    }

                }else if(sellected == 1){ //Z-Top
                    BringtoTop(0);
                }else if(sellected == 2){ //Z-Bottom
                    SentToBack(0);
                }else if(sellected == 3){ //Toggle visibility
                    if(myCircle != null){
                        boolean vissible = !myCircle.isVisible();
                        System.out.println("set myCircle visible: " + vissible);
                        myCircle.setVisible(vissible);
                    }
                }else if(sellected == 4){ //Zoom to
                    if(myCircle != null){
                        System.out.println("get myCircle box");
                        GeoBoundingBox box0 = myCircle.getBoundingBox();
                        if(box0 != null){        
                            mapCanvas.getMapDisplay().zoomTo(box0, false); 
                        }else{
                            System.out.println("myCircle box is NULL");
                        }
                    }
                }else if(sellected == 5){ //Remove 
                    if(myCircle != null){
                        mapCanvas.getMapDisplay().removeMapObject(myCircle);
                        myCircle = null;
                        System.out.println("removed myCircle");
                    }
                }else if(sellected == 6){ //Toggle position
                    if(myCircle != null){
                        
                        if(chnagedPos){
                            chnagedPos = false;
                            myCircle.setCenter(new GeoCoordinate(60.30, 24.70,0)); 
                        }else{
                            chnagedPos = true;
                            myCircle.setCenter(new GeoCoordinate(60.35, 24.60,0)); 
                        }
                        System.out.println("myCircle position chnaged: " + chnagedPos);
                    }
                }else if(sellected == 7){ //Toggle size 
                    if(myCircle != null){
                        
                        if(myCircle.getRadius() > 3000){
                            myCircle.setRadius(2500);
                        }else{
                            myCircle.setRadius(5000);
                        }
                         
                        System.out.println("myCircle size changed to: " + myCircle.getRadius());
                    }
                }else {
                    Alert alert = new Alert("", "Uknown command", null, AlertType.INFO);    
                    display.setCurrent(alert);
                }  
                
                
                        
            }else if(selllected == 1){//Rectangle
                if(sellected == 0){ //Add
                    if(rectnagle == null){
                        
                        GeoBoundingBox rectnaglebox = new GeoBoundingBox(new GeoCoordinate(60.35, 24.60,0), new GeoCoordinate(60.25, 24.80,0));
              
                        rectnagle = mapCanvas.getMapFactory().createMapRectangle(rectnaglebox);
                        rectnagle.setColor(0xAA0506FF);
                        
                        mapCanvas.getMapDisplay().addMapObject(rectnagle);
                        System.out.println("rectnagle added");
                    }else{//already added   
                    }      
                }else if(sellected == 1){ //Z-Top
                    BringtoTop(1);
                }else if(sellected == 2){ //Z-Bottom
                    SentToBack(1);
                }else if(sellected == 3){ //Toggle visibility
                    if(rectnagle != null){
                        boolean vissible = !rectnagle.isVisible();
                        System.out.println("set rectnagle visible: " + vissible);
                        rectnagle.setVisible(vissible);
                    }
                }else if(sellected == 4){ //Zoom to
                    if(rectnagle != null){
                        System.out.println("get rectnagle box");
                        GeoBoundingBox box1 = rectnagle.getBoundingBox();
                        if(box1 != null){        
                            mapCanvas.getMapDisplay().zoomTo(box1, false); 
                        }else{
                            System.out.println("rectnagle box is NULL");
                        }
                    }
                }else if(sellected == 5){ //Remove 
                    if(rectnagle != null){
                        mapCanvas.getMapDisplay().removeMapObject(rectnagle);
                        rectnagle = null;
                        System.out.println("removed rectnagle");
                    }
                }else {
                    Alert alert = new Alert("", "Uknown command", null, AlertType.INFO);    
                    display.setCurrent(alert);
                }
            }else{
                Alert alert = new Alert("", "Uknown Selection", null, AlertType.INFO);    
                display.setCurrent(alert);
            }
            
            display.setCurrent(mapCanvas);
        }else{
            Alert alert = new Alert("", "Uknown command", null, AlertType.INFO);    
            display.setCurrent(alert);
        }
    }

    
    public void SentToBack(int which) {

        int markerZ = 0;
        int polyZ = 0;

        if (myCircle != null){
            markerZ = myCircle.getzIndex();
        }


        if (rectnagle != null){
            polyZ = rectnagle.getzIndex();
        }

        if (which == 0 )//"Markers"
        {
            if (markerZ >= polyZ)
                markerZ = polyZ - 1;
        }else if(which == 1)//"rectnagle"
        {
            if (polyZ >= markerZ)
                polyZ = markerZ - 1;
        }
        
        if (myCircle != null){
            myCircle.setzIndex(markerZ);
        }

        if (rectnagle != null){
            rectnagle.setzIndex(polyZ);
        }

        System.out.println("Z-order, myCircle: " + markerZ + ", rectnagle: " + polyZ);
    }

    public void BringtoTop(int which) {

        int markerZ = 0;
        int polyZ = 0;

        if (myCircle != null){
            markerZ = myCircle.getzIndex();
        }

        if (rectnagle != null){
            polyZ = rectnagle.getzIndex();
        }

        if (which == 0 )//"Markers"
        {
            if (markerZ <= polyZ)
                markerZ = polyZ + 1;
        }else if(which == 1)//"rectnagle"
        {
            if (polyZ <= markerZ)
                polyZ = markerZ + 1;
        }

        if (myCircle != null){
            myCircle.setzIndex(markerZ);
        }

        if (rectnagle != null){
            rectnagle.setzIndex(polyZ);
        }

    System.out.println("Z-order, myCircle: " + markerZ +  ", rectnagle: " + polyZ);
    }
     
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}
